# Definition: at3_xrootdhead
#
#  Xrootd is a high performance network storage system widely used in high energy 
#  physics experiments such as ATLAS and ALICE. The underlying Xroot data transfer 
#  protocol provides highly efficient access to ROOT based data files. This class provides 
#  the basis for creating a simple Xrootd storage system consisting of one 
#  redirector node and one or more data server nodes.
#  
#  This is the configuration for the ATLAS Tier 3 head node it has a redirector and a
#  data server.
#
# Parameters:
#   - $fqdnRedirector : fully qualified domain name for the redirector
#   - $inventory: inventory directory for the Simple Service Inventory. 
#      Default value is'/atlas/inventory'    
#   - $ossLocalRoot: Local root for the data server inventories. Default is'/local/xrootd/a'
#   - $storagePath: a directory containing internal Xrootd references to files in the system.
#      Default is '/atlas'
#   - $fuse - should there be a fuse mount to xrootd (true or false). Default is false.
#   - $mountPoint - what is the fuse mount point. Default is '/xrootdfs/atlas'
#   
# Actions:
#   - installs xrootd-server
#   - sets up the storage directorys, prepares configuration files for xrootd and the
#     the simple storage inventory
#   - starts the xrootd and cmsd services
#
# Requires:
#  
#   
define at3_xrootdhead ($fqdnRedirector,$inventory='/atlas/inventory',$ossLocalRoot='/local/xrootd/a',$storagePath='/atlas',$fuse=false, $mountPoint='/xrootdfs/atlas') 
{
  $version='5'
  $basearch='x86_64'
  yumrepo { 'xrootd-stable':
    descr    => 'XRootD Stable repository',
    baseurl  => "http://xrootd.org/binaries/stable/slc/${version}/${basearch} http://xrootd.cern.ch/sw/repos/stable/slc/${version}/${basearch}",
    gpgcheck => '0',
    enabled  => '1',
    priority => '1',
  }

  file { "/etc/xrootd/xrootd-clustered.cfg":
    ensure 	=> present,
    content	=> template("at3_xrootdhead/xrootd-clustered.cfg.tpl"),
    require	=> Exec['setup-xrootd-server'],
  } 

  exec { 'install-xrootd-server':
    command => 'yum -y --disablerepo=epel install xrootd-server',
    path => ['/bin','/usr/bin'],
    creates => '/usr/bin/xrootd', 
    require => Yumrepo['xrootd-stable'],
    logoutput => true,
  }


  exec { 'setup-xrootd-server':
    command 	=> 'service xrootd setup',
    path 	=> ['/sbin','/usr/bin'],
    creates     => ["/etc/xrootd/xrootd-clustered.cfg"],
    require 	=> Exec['install-xrootd-server'],
    logoutput	=> true,
  }

  file { '/etc/sysconfig/xrootd':
    ensure 	=> present,
    content	=> template('at3_xrootdhead/xrootd.tpl'),
    owner	=> 'xrootd',
    group	=> 'xrootd',
    require => Exec['setup-xrootd-server'],
  } 

  #  a directory containing internal Xrootd references to files in the system
  exec { "$storagePath":
    command	=> "mkdir -p $storagePath",
    path	=> ['/bin','/usr/bin'],
    creates	=> "$storagePath",
    require => Exec['setup-xrootd-server'],
  }
    
  # The Simple Server Inventory (SSI) provide means to have an inventory for 
  # each data server (See details in xrootd.org/doc/dev/cms_config.pdf). In 
  # order to configure it you will need to run a second instance of xrootd 
  # daemon as well XrdCnsd process that should run on every data server. We 
  # will configure xrootd cluster that consists of two nodes. Host A is a 
  # redirector node that is running the following daemons
  exec { "$inventory":
    command	=> "mkdir -p $inventory",
    path	=> ['/bin','/usr/bin'],
    creates	=> "$inventory",
    require => Exec['setup-xrootd-server'],
  }

  # Sets the ownership of the $inventory and $storagePath
  # to the xrootd user
  exec { "chown -R xrootd:xrootd $inventory":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    require => [Exec['setup-xrootd-server'],Exec["$inventory"]],
  }
  exec { "chown -R xrootd:xrootd $storagePath":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    require => [Exec['setup-xrootd-server'],Exec["$storagePath"]],
  }

  # Start the services 
  service { 'xrootd':
    name   => 'xrootd',
    ensure => 'running',
    enable => true,
    pattern => '/usr/bin/xrootd',
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/sysconfig/xrootd'],Exec["$storagePath"],Exec["$inventory"]],
   }

  service { 'cmsd':
    name   => 'cmsd',
    ensure => 'running',
    enable => true,
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/sysconfig/xrootd'],Exec["$storagePath"],Exec["$inventory"]],
   }
   
   # Check to see if a fuse mount is desired
   if $fuse {
     
     # recursively make all the directories for the mount point 
     exec { "$mountPoint":
       command => "mkdir -p $mountPoint",
       creates => "$mountPoint",
       path    => ['/bin','/usr/bin'],
       require => [Service['xrootd']],
       onlyif => ["test -z `mount | grep xrootdfs`"],
       logoutput => true,
     } 
 
     # set the ownership of the directory to the xrootd user
     exec { "chown -R xrootd:xrootd $mountPoint":
       path    => ['/bin','/usr/bin'],
       require => Exec["$mountPoint"],
       logoutput => true,
       onlyif => ["test -z `mount | grep xrootdfs`"],
     } 
 
     exec { 'install-xrootd-fuse':
      command => 'yum -y --disablerepo=epel install xrootd-fuse',
      path => ['/bin','/usr/bin'],
      creates => '/usr/bin/xrootdfs', 
      require => [Service['cmsd'],Service['xrootd']],
      logoutput => true,
     }

     # mount it.
     mount { "$mountPoint":
       atboot	=> true,
       device	=> "xrootdfs",
       dump	=> 0,
       ensure	=> mounted,
       fstype	=> fuse,
       options	=> "nonempty,rdr=xroot://${fqdnRedirector}:1094/${storagePath},uid=xrootd",
       pass	=> 0,
       remounts  => false,
       require	=> [Exec['install-xrootd-fuse'],Exec["$mountPoint"]],
     } 

   }
  
}
